package excuter;

import hadoop.classification.linear.HadoopLR;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import standalone.classification.linear.LogisticRegression;

/**
 * Executer
 * @author Erheng Zhong (purlin.zhong@gmail.com)
 *
 */
public class LogisticRegressionRunner {
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String inputPath = null, outputPath = null, modelName = null, fileType = "libsvm";
		String master = "localhost";
		int masterPort = 10090;
		int numFeatures=0, numClasses=2, numIterations=1000;
		float learningRate = 0.05f, eps = 0.001f, d = 0.1f;
		// Create a Parser
		CommandLineParser parser = new BasicParser();
		Options options = new Options();
		options.addOption("h", "help", false, "Print this usage information");
		options.addOption("H", "hadoop", false, "Hadoop or standalone");
		options.addOption("b", "model", true, "Model Name");
		options.addOption("d", "regularization", true, "Regularization parameter");
		options.addOption("i", "input", true, "Path of training data");
		options.addOption("o", "output", true, "Path of model");
		options.addOption("F", "filetype", true, "File type");
		options.addOption("f", "features", true, "Number of features");
		options.addOption("c", "classes", true, "Number of classes");
		options.addOption("t", "iterations", true, "Number of iterations");
		options.addOption("r", "rate", true, "Learning rate");
		options.addOption("e", "eps", true, "Termination threshold");
		options.addOption("m", "master", true, "IP address of spanning tree");
		options.addOption("p", "port of master", true, "Port number");
		HelpFormatter formatter = new HelpFormatter();
		// Parse the program arguments
		CommandLine cmd = parser.parse(options, args);
		if (cmd.hasOption("i")) inputPath = cmd.getOptionValue("i");
		else { formatter.printHelp("ant", options); return; }
		if (cmd.hasOption("o")) outputPath = cmd.getOptionValue("o");
		else { formatter.printHelp("ant", options); return; }
		if (cmd.hasOption("b")) modelName = cmd.getOptionValue("b");
		else { formatter.printHelp("ant", options); return; }
		if (cmd.hasOption("d")) d = Float.parseFloat(cmd.getOptionValue("d"));
		if (cmd.hasOption("t")) numIterations = Integer.parseInt(cmd.getOptionValue("t"));
		if (cmd.hasOption("r")) learningRate = Float.parseFloat(cmd.getOptionValue("r"));
		if (cmd.hasOption("e")) eps = Float.parseFloat(cmd.getOptionValue("e"));
		if (cmd.hasOption("f")) numFeatures = Integer.parseInt(cmd.getOptionValue("f"));
		if (cmd.hasOption("c")) numClasses = Integer.parseInt(cmd.getOptionValue("c"));
		if (cmd.hasOption("m")) master = cmd.getOptionValue("m");
		if (cmd.hasOption("p")) masterPort = Integer.parseInt(cmd.getOptionValue("p"));
		if (cmd.hasOption("F")) fileType = cmd.getOptionValue("F");
		// Main process
		if(cmd.hasOption("H")) HadoopLR.buildModel(inputPath, outputPath, modelName, d, numFeatures, numClasses, numIterations, learningRate, eps, master, masterPort);
		else LogisticRegression.buildModel(inputPath, outputPath, fileType, modelName, d, numFeatures, numClasses, numIterations, learningRate, eps, master, masterPort);
	}

}